package shopping

import (
	"errors"
	"strings"
	"time"
	"dhfshop/models"
	"github.com/astaxie/beego/orm"
)

//评价
type Review struct {
	ReviewId   int64     `orm:"column(review_id);pk;auto" json:"review_id" form:"review_id"` 
	PartnerId  int64     `orm:"column(partner_id)" json:"partner_id" form:"partner_id"`       //
	OrderId    int64     `orm:"column(order_id)" json:"order_id,string" form:"-"`               //交易ID
	OrderIdStr string    `orm:"-" json:"-" form:"order_id"` /*JS中整数范围是 -2^53~2^53, 即：9.007199254741*10^15，超过15位不安全*/
    MemberId   int64     `orm:"column(member_id)" json:"member_id" form:"member_id"`     /* 买家id */
	ItemId     int64     `orm:"column(item_id)" json:"item_id,string" form:"-"`    //商品ID
    SkuId      int64     `orm:"column(sku_id)" json:"sku_id,string" form:"-"`     //规格ID
    ItemIdStr  string    `orm:"-" json:"-" form:"item_id"`
    SkuIdStr   string    `orm:"-" json:"-" form:"sku_id"`
	Score      int       `orm:"column(score)" json:"score" form:"score"`         //评分: 1:好; 0:中; -1:差
	Content    string    `orm:"column(content);type(text)" json:"content" form:"content"`   //评论内容
	Image      string    `orm:"column(image)" json:"image" form:"image"`       //评论的图片 
	CreatedAt  time.Time `orm:"auto_now_add;column(created_at);type(datetime)" json:"created_at" form:"-"`   /* 创建时间 */	
}

func init() {
    orm.RegisterModel(new(Review))   
}

func AddReview(m *Review) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func AddMultiReviews(list []*Review) (err error) {
    o := models.GetDefaultDB()
    size := len(list)
	if size > 1 {
	    _, err = o.InsertMulti(size, list) 
	}else{
	   _, err = o.Insert(list[0])
	}
	return
}

func GetReviewById(id int64) (v *Review, err error) {
	o := models.GetDefaultDB()
	v = &Review{ReviewId: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetAllReviews(query map[string]string, fields []string, order_by map[string]string, 
    offset int, limit int) ([]*Review, int64,error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(Review))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		qs = qs.Filter(k, v)
	}
	cnt, err := qs.Count()
	// order by:
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	var lists []*Review
	if _, err := qs.Limit(limit, offset).All(&lists, fields...); err == nil {
		return lists, cnt, nil
	}
	return nil, 0, err
}

func UpdateReviewById(m *Review, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := Review{ReviewId: m.ReviewId}
	if err = o.Read(&v); err != nil {
		return err
	}else{
	    if _, err = o.Update(m, k...); err != nil {
		   return err
		}
	}
	return nil
}

func DeleteReview(id int64) (err error) {
	o := models.GetDefaultDB()
	v := Review{ReviewId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}

